package com.ljkj.cordial.chat.base;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.text.TextUtils;

import com.lanjiu.lib.app.App;
import com.lanjiu.lib.utils.json.JsonUtils;
import com.ljkj.cordial.chat.BuildConfig;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.activity.SplashActivity;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.FontSize;
import com.ljkj.cordial.chat.dao.UserTableBean;
import com.ljkj.cordial.chat.widget.MediaLoader;
import com.nic.view.ViewConfig;
import com.nic.view.util.SharedUtils;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumConfig;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ChatApp extends App {
    private static ChatApp instance;
    private List<Activity> activityList = new ArrayList<>();
    private UserTableBean user;

    public void addActivity(Activity activity) {
        if (activity == null || activity.isFinishing()) {
            return;
        }
        activityList.add(activity);
    }

    public void removeActivity(Activity activity) {
        if (activityList.isEmpty()) {
            return;
        }
        if (activityList.contains(activity)) {
            activityList.remove(activity);
        }
    }

    public void quitApp() {
        for (Activity activity : activityList) {
            if (activity instanceof SplashActivity) {
                continue;
            }
            activity.finish();
        }
//        System.exit(0);
    }

    public void quitApp(Activity noFinishActivity) {
        for (Activity activity : activityList) {
            if (activity.getClass().getName().equals(noFinishActivity.getClass().getName())) {
                continue;
            }
            activity.finish();
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public static ChatApp getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        ViewConfig.init(this, getPackageName(),
                Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + BuildConfig.APPLICATION_ID
                , BuildConfig.DEBUG);
        Album.initialize(AlbumConfig.newBuilder(this)
                .setAlbumLoader(new MediaLoader())
                .setLocale(Locale.getDefault())
                .build()
        );

//        EmojiPackage.initEmojis(this);assets、emoji无用图标可删除
    }


    public float getFontSize() {
        return (float) SharedUtils.get(getInstance(), Canstants.KEY_FONT_SIZE, FontSize.FONT_NORMAL);
    }

    public void setFontSize(float fontSize) {
        SharedUtils.put(getInstance(), Canstants.KEY_FONT_SIZE, fontSize);
    }

    /**
     * min 0.85 normal 1.0 mid 1.15 max 1.3
     *
     * @return
     */

    public String getRegisterId() {
        return get() != null ? get().getUserId() : "";//(String) SharedUtils.get(getApplicationContext(), Canstants.KEY_UserId, "");
    }

    public String getTouristsId() {
        return get() != null ? get().getTemporaryId() : "";//(String) SharedUtils.get(getApplicationContext(), Canstants.KEY_VisitorId, "");
    }

    public String getUserId() {
//        if (isRegister()) {
//            return getRegisterId();
//        } else {
//            return getTouristsId();
//        }
        return getRegisterId();
    }

    public boolean isRegister() {
//        String type = (String) SharedUtils.get(getApplicationContext(), Canstants.KEY_UseType, "");
//        if (UserTableBean.TYPE_REGISTER.equals(type)) {
//            return true;
//        }
        if (get() != null && UserTableBean.TYPE_REGISTER.equals(get().getUserType())) {
            return true;
        }
        return false;
    }

    public String getUserType() {
//        String type = (String) SharedUtils.get(getApplicationContext(), Canstants.KEY_UseType, "");
        if (get() != null) {
            return get().getUserType();
        }
        return UserTableBean.TYPE_REGISTER;
    }

//    public void initU() {
//        try {
//            UserTableBean u = UserTableBean.getCurrentUser();
//            if (u == null) {
//                return;
//            }
//            SharedUtils.put(this, Canstants.KEY_Nickname, u.getNickname());
//            SharedUtils.put(this, Canstants.KEY_Avatar, u.getAvatar());
//        } catch (Exception e) {
//        }
//
//    }

    public void put(UserTableBean user) {
        remove();
        SharedUtils.put(this, Canstants.KEY_USER_INFO, JsonUtils.toJson(user));
    }

    public void remove() {
        user = null;
        SharedUtils.remove(this, Canstants.KEY_USER_INFO);
    }

    public UserTableBean get() {
        if (user == null) {
            String u = (String) SharedUtils.get(this, Canstants.KEY_USER_INFO, "");
            if (TextUtils.isEmpty(u)) {
                return null;
            }
            user = JsonUtils.fromJson(u, UserTableBean.class);
        }

        return user;
    }

    public boolean isExit() {
        return (boolean) SharedUtils.get(this, Canstants.KEY_USER_INFO_EXIT, false);
    }

    public void setExitFlag(boolean flag) {
        SharedUtils.put(this, Canstants.KEY_USER_INFO_EXIT, flag);
    }

    public void inActivityAnim(Context context) {
        ((Activity) context).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
}
